/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @file
 * @brief Interface of the generic class stack.
 *
 * The stack class is just a simple wrapper of amcc_list.
 *
 * @author  WXQ#XDU
 * @date    2024.11
 */

#pragma once

#ifndef AMCC_STACK_H
#define AMCC_STACK_H

#include "amcc_list.h"

AMCC_DECL_BEGIN


//
// Hi, class stack is based on amcc_list
//


typedef t_list                       t_stack;
typedef t_list_ptr                   t_stack_ptr;
typedef t_list_app_value             t_stack_app_value;
typedef t_list_node                  t_stack_node;
typedef t_listfunc_free_appdata      t_stackfunc_free_appdata;
typedef t_listfunc_visit_appdata     t_stackfunc_visit_appdata;

#define stack_init(pStack, fp_free)  list_init(pStack, fp_free)
#define stack_clean(pStack)          list_clean(pStack)

#define stack_is_empty(pStack)       list_is_empty(pStack)
#define stack_size(pStack)           list_size(pStack)

// traversal from bottom to top
#define stack_foreach(pStack, fp_visit, user_data) list_foreach(pStack,fp_visit,user_data)

// traversal from top to bottom
#define stack_foreach_reverse(pStack, fp_visit, user_data) list_foreach_reverse(pStack,fp_visit,user_data)


#define stack_push(pStack, appData)  list_append(pStack, appData)
#define stack_pop(pStack)            list_remove_last(pStack)
#define stack_top(pStack)            list_get_last_value(pStack)


//
// Hi, class stack iterator is based on amcc_list and its iterator
//


typedef t_list_iterator              t_stack_iterator;

#define STACK_ITERATOR_INIT(pIter, pList)  LIST_ITERATOR_INIT(pIter, pList)

// traversal from bottom to top: BEGIN, END, NEXT.
// Prefer stack_foreach() if it can meet your requirements.
#define STACK_ITERATOR_BEGIN(pIter)        LIST_ITERATOR_BEGIN(pIter)
#define STACK_ITERATOR_END(pIter)          LIST_ITERATOR_END(pIter)
#define STACK_ITERATOR_NEXT(pIter)         LIST_ITERATOR_NEXT(pIter)

// traversal from top to bottom: RBEGIN, REND, PREV.
// Prefer stack_foreach_reverse() if it can meet your requirements.
#define STACK_ITERATOR_RBEGIN(pIter)       LIST_ITERATOR_RBEGIN(pIter)
#define STACK_ITERATOR_REND(pIter)         LIST_ITERATOR_REND(pIter)
#define STACK_ITERATOR_PREV(pIter)         LIST_ITERATOR_PREV(pIter)


AMCC_DECL_END

#endif  // #ifndef AMCC_STACK_H
